package handlers

import (
	"github.com/gin-gonic/gin"
	"net/http"
	"sanrenx.com/xyxf/app/services"
	"sanrenx.com/xyxf/pkg/app"
	"sanrenx.com/xyxf/pkg/e"
	"sanrenx.com/xyxf/pkg/logging"
	"strings"
)

// @Tags 分类
// @Summary 获取分类列表
// @Accept application/json
// @Produce  json
// @Param mode query string true "类型"
// @Param pid query integer false "父ID"
// @Success 200 {object} app.Response
// @Router  /app/v2/category [get]
func Category(c *gin.Context) {
	var (
		dataSrv services.CategorySrv
		params  struct {
			Mode string `form:"mode" binding:"required"`
			PID  int    `form:"pid"`
		}
	)
	appG := app.Gin{C: c}
	//校验绑定参数
	if err := c.ShouldBind(&params); err != nil {
		app.MarkErrors(err)
		appG.Response(http.StatusBadRequest, e.InvalidParams, nil)
		return
	}

	//切割模式类型 支持多模式共同返回
	modes := strings.Split(params.Mode, ",")
	maps := make(map[string]interface{})
	for _, mode := range modes {
		var err error
		if maps[mode], err = dataSrv.GetCategoriesByMode(mode, params.PID); err != nil {
			logging.Warn(err)
			continue
		}
	}

	appG.Response(http.StatusOK, e.SUCCESS, maps)
}
